package com.fhzn.boss.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzn.boss.entity.App;
import com.fhzn.boss.web.converter.AppConverter;
import com.fhzn.boss.web.request.AppQueryRequest;
import com.fhzn.boss.web.vo.AppExportVO;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author 老顾
 * @apiNote
 * @since 2024/1/3
 */
@Mapper
public interface AppMapper extends BaseMapper<App> {


    /**
     * 获取应用列表
     *
     * @param request 请求参数
     * @return 返回参数
     */
    default Page<App> getAppsWithPage(AppQueryRequest request) {
        LambdaQueryWrapper<App> wrapper = Wrappers.lambdaQuery();
        if (request.getName() != null) {
            wrapper.like(App::getName, request.getName());
        }
        if (request.getStatus() != null) {
            wrapper.eq(App::getStatus, request.getStatus());
        }
        wrapper.orderByAsc(App::getId);
        return selectPage(Page.of(request.getPage(), request.getPageSize()), wrapper);
    }


    /**
     * 根据code获取应用
     *
     * @param code code
     * @return 返回应用
     */
    default AppExportVO findByCode(String code) {
        App apps = selectOne(Wrappers.lambdaQuery(App.class).eq(App::getCode, code));
        return AppConverter.fromExportApp(apps);
    }
}
